#!/bin/bash
#########################################################################################
# Copyright (c) 2014 VistaraIT, Inc. All rights reserved.                               #
# File Name                     : prepare_logs.sh                                       #
# Purpose                       : To retrieve the Vistara node logs                     #
#                               :                                                       #
#                               :                                                       #
#                               :                                                       #
# Version                       : 1.0                                                   #
# Revision                      :                                                       #
# Original Author               : Ram Murthy                                            #
# Date                          : Sep 30, 2014                                          #
# Modification details          :                                                       #
# Reviewed By                   :                                                       #
# Tested by                     :                                                       #
#########################################################################################

export LANG=C

# Check we're running as root
if [ "$(id -u)" != "0" ]; then
  echo "The update script must be run as root"
  exit 1
fi

BASEDIR=$(dirname $0)
cd $BASEDIR

if [ "$#" -ne 1 ] || ! [ "$1" == "db" -o "$1" == "itop" -o "$1" == "cn" -o "$1" == "rdp2swf" -o "$1" == "se" ] 
then
  echo 
  echo "Please provide the server type to prepare logs. [db|itop|cn|rdp2swf|se]"
  echo "Usage: $0 <db|itop|cn|rdp2swf|se>"
  echo
  exit 1
fi
LOGFILE="$BASEDIR/backup.log";

prepare_logs() {
  while true; do
    echo -n "Do you want to collect log files on itop-$1 node (y/n)>"
    read choice
    echo
    case $choice in
      [yY] | [yY][Ee][Ss])
        break
        ;;
      [nN] | [n|N][O|o] )
        echo "Not done, you can't proceed further";
        exit 0
        ;;
      *)
        echo "That is not a valid choice, try again"
        ;;
    esac
  done

  echo "Starting log process..." | tee -a $LOGFILE
if ! [ -d /tmp/node/ ]
  then
    mkdir /tmp/node/
  fi
  INFOFILE="/tmp/node/Info.txt";
  TAR_FILE="/home/itopadmin/itop-$1-logs.tgz";
  echo "Vistara $1 node information" > $INFOFILE 2>&1
  echo "Hostname : `hostname`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "ifconfig : `ifconfig`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE 2>&1
  echo "netstat -nl : `netstat -nl`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "iptables-save : `iptables-save`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "ip route : `ip route`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "monit summary : `monit summary`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "date : `date`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "mount : `mount`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "uptime : `uptime`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "top -n1 -b : `top -n1 -b`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  
  echo "free -m : `free -m`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "/bin/df -h : `/bin/df -h`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "/bin/ps auefx : `/bin/ps auefx`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "/bin/netstat -an : `/bin/netstat -an`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "/bin/netstat -rn : `/bin/netstat -rn`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "/usr/sbin/ufw status verbose : `/usr/sbin/ufw status verbose`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  echo "/usr/bin/crontab -l : `/usr/bin/crontab -l`" >> $INFOFILE 2>&1
  echo  >> $INFOFILE
  
  if [ "$1" == "itop" ]
  then
	echo "PROCESS - " >> $INFOFILE 2>&1
	echo "ps -ef | grep itop : `ps -ef | grep itop`" >> $INFOFILE 2>&1
	echo  >> $INFOFILE
  fi

  if [ "$1" == "cn" -o "$1" == "rdp2swf" -o "$1" == "se" ]
  then
	echo "PROCESS - " >> $INFOFILE 2>&1
	echo "ps -ef | grep ncare : `ps -ef | grep ncare`" >> $INFOFILE 2>&1
	echo  >> $INFOFILE
  fi

  if [ "$1" == "db" ]
  then
	echo "PROCESS - " >> $INFOFILE 2>&1
	echo "ps -ef | grep mysql : `ps -ef | grep mysql`" >> $INFOFILE 2>&1
	echo  >> $INFOFILE
	echo "ps -ef | grep mongo : `ps -ef | grep mongo`" >> $INFOFILE 2>&1
	echo  >> $INFOFILE
	echo "ps -ef | grep kafka : `ps -ef | grep kafka`" >> $INFOFILE 2>&1
	echo  >> $INFOFILE
	echo "ps -ef | grep zk : `ps -ef | grep zk`" >> $INFOFILE 2>&1
	echo  >> $INFOFILE
	echo "ps -ef | grep cassandra : `ps -ef | grep cassandra`" >> $INFOFILE 2>&1
	echo  >> $INFOFILE
	
	/usr/bin/mysqldump -uroot -pxAVa6Aze msp nda_nodes > /tmp/node/nda_nodes.sql --single-transaction
	/usr/bin/mysqldump -uroot -pxAVa6Aze msp cpe_conn_server > /tmp/node/cpe_conn_server.sql --single-transaction
	/usr/bin/mysqldump -uroot -pxAVa6Aze msp cpe_management_gateway_profile > /tmp/node/cpe_management_gateway_profile.sql --single-transaction
	/usr/bin/mysqldump -uroot -pxAVa6Aze msp cpe_management_gateway > /tmp/node/cpe_management_gateway.sql --single-transaction
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="SHOW GLOBAL STATUS" > /tmp/node/global_status.txt;
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="SHOW SESSION STATUS" > /tmp/node/session_status.txt;
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="show full processlist" > /tmp/node/full_processlist.txt;
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="show session variables" > /tmp/node/session_variables.txt;
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="select * from tables" > /tmp/node/tables_list.txt;
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="select * from USER_PRIVILEGES" > /tmp/node/user-previleges.txt;
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="SHOW ENGINE INNODB STATUS" > /tmp/node/engine_innodb_status.txt
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="select * from INNODB_SYS_TABLESTATS" > /tmp/node/innodb_sys_tablestats.txt
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="select * from INNODB_LOCK_WAITS" > /tmp/node/innodb_lock_waits.txt
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="select * from INNODB_TRX" > /tmp/node/innodb_trx.txt
	/usr/bin/mysql --user="root" --password="xAVa6Aze" --database="information_schema" --execute="select * from INNODB_LOCKS" > /tmp/node/innodb_locks.txt
  fi

  if [ -f $TAR_FILE ]
  then
	rm $TAR_FILE
  fi

/bin/tar --ignore-failed-read -cvjf $TAR_FILE /var/log/syslog* /var/log/kern* /var/log/boot* /var/log/dmesg* /var/log/mysql* \
/var/log/memcached.log* /var/log/apache2* /var/log/monit.log* /var/log/cassandra /var/log/kafka /var/log/mongodb /var/log/mysql \
/var/log/zk /var/log/itop /opt/itop/logs /opt/itop/conf /var/log/ncare /opt/ncare/conf /home/itopadmin/itop-upgrade/install.log \
/etc/fstab /etc/ntp.conf /etc/resolv.conf /etc/mail/sendmail.cf /etc/mail/submit.cf /etc/snmp/snmpd.conf /var/log/auth.log* /var/log/mail.err* \
/var/log/mail.log* /etc/cassandra/* /etc/mongodb.conf /etc/mysql/* /opt/kafka/config/* /opt/kafka/logs /etc/network/interfaces \
/etc/hosts /etc/hostname /etc/ufw/before.rules /tmp/node  >> $INFOFILE 2>&1

  rm -fr /tmp/node/
  echo "Tar ball - $TAR_FILE..." | tee -a $LOGFILE
  echo "Backup logs completed..." | tee -a $LOGFILE
  return 0
}

case "$1" in
  cn)
    if ! [ -f /etc/monit/conf.d/ncare.monitrc ]
    then
      echo "It seems you run the script on this node with wrong server type.. so aborting.."
      exit 1
    fi
    prepare_logs "cn"
    ;;
   
  rdp2swf)
    if ! [ -f /etc/monit/conf.d/rdp2swf.monitrc ]
    then
      echo "It seems you run the script on this node with wrong server type.. so aborting.."
      exit 1
    fi
    prepare_logs "rdp2swf"
    ;;
   
  se)
    if ! [ -f /etc/monit/conf.d/searchengine.monitrc ]
    then
      echo "It seems you run the script on this node with wrong server type.. so aborting.."
      exit 1
    fi
    prepare_logs "se"
    ;;
    
  db)
    prepare_logs "db"
    ;;
    
  itop)
    if ! [ -f /etc/monit/conf.d/itop.monitrc ]
    then
      echo "It seems you run the script on this node with wrong server type.. so aborting.."
      exit 1
    fi
    prepare_logs "itop"
    ;;
  *)
    echo "Usage: $0 {db|itop|cn|rdp2swf|se}"
    exit 1
    ;;
esac

exit $?
